package com.yuda.job.task;

import cn.hutool.extra.ssh.JschUtil;
import com.jcraft.jsch.Session;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;

@Component("copyFile")
public class SunnyCopyFileTask {

    public void exec() {
        Session session = null;
        try {
            SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
            String format = yyyyMMdd.format(System.currentTimeMillis() - 24 * 60 * 68 * 1000);
            session = JschUtil.getSession("182.92.197.32", 22, "root", "Thussat@2021");
            System.out.println("session.isConnected() ===  >  " + session.isConnected());
            System.out.println(JschUtil.exec(session, "cd", StandardCharsets.UTF_8));
            System.out.println(JschUtil.exec(session, "./copykey.sh " + format, StandardCharsets.UTF_8));
            System.out.println("执行命令完毕...");
        } finally {
            if (session != null) {
                JschUtil.close(session);
            }
        }
    }
}
